package com.itboye.hutouben.activity.store;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.hutouben.R;
import com.itboye.hutouben.adapter.GoodsListAdapter;
import com.itboye.hutouben.adapter.ZuJinAdapter;
import com.itboye.hutouben.base.BaseActivity;
import com.itboye.hutouben.base.ptr.BasePtr;
import com.itboye.hutouben.bean.GoodsModel;
import com.itboye.hutouben.bean.PaiXuBean;
import com.itboye.hutouben.logincontroller.LoginController;
import com.itboye.hutouben.presenter.ShopMallSearchPresenter;
import com.itboye.hutouben.util.ByAlert;
import com.itboye.hutouben.util.IsUtilUid;
import com.itboye.hutouben.volley.ResultEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * 商品列表
 */
public class GoodsListActivity extends BaseActivity implements Observer {

    GoodsListAdapter adapter;
    ImageView img_other, close_icon;
    private List<GoodsModel.GoodsList> list = new ArrayList<>();
    GoodsModel shopMallSearchBean = new GoodsModel();
    GridView goods_gridView;
    TextView add_shap_title_tv;
    private PopupWindow popupWindow;
    RelativeLayout re_shaixuan, re_paixu;
    int temp_paixu, temp_shaixuan;//记录条件的左边，更换是否选中的值
    ShopMallSearchPresenter presenter;
    int pageIndex = 1;//页码
    int size = 10;//每页的条数
    RelativeLayout null_search;
    LinearLayout tiaojian_lin;
    PtrFrameLayout commend_anchor_ptr;
    String order = "d";//排序(d=>'默认',pd=>价格从高到低,pa=>价格从低到高)可选

    String l_price = "", r_price = "";//上限价格，下限价格
    String cate_id;//上个页面传过来的类目id
    String searchText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_list);
        add_shap_title_tv.setText(getString(R.string.qipeishangcheng));
        cate_id = getIntent().getStringExtra("cate_id");
        BasePtr.setPagedPtrStyle(commend_anchor_ptr);//设置上拉下拉刷新
        img_other.setBackgroundResource(R.drawable.gouwuche);
        img_other.setVisibility(View.VISIBLE);

        presenter = new ShopMallSearchPresenter(this);
        presenter.shangPinList(pageIndex + "", size + "", cate_id, "", order, "", l_price, r_price);
        list = new ArrayList<GoodsModel.GoodsList>();
        adapter = new GoodsListAdapter(list, this);
        goods_gridView.setAdapter(adapter);
        commend_anchor_ptr.setPtrHandler(new PtrDefaultHandler2() {
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                pageIndex++;
                presenter.shangPinList(pageIndex + "", size + "", cate_id, "", order, "", l_price, r_price);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                presenter.shangPinList(pageIndex + "", size + "", cate_id, "", order, "", l_price, r_price);
            }
        });

        goods_gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(GoodsListActivity.this, GoodDetailActivity.class);
                intent.putExtra("goodsId", list.get(position).getId());
                intent.putExtra("goodsName", list.get(position).getName());
                if (list.get(position).getPrice().equals("")) {
                    intent.putExtra("money", "");
                } else {
                    intent.putExtra("money", Double.parseDouble(list.get(position).getPrice()) / 100 + "");
                }
                if (list.get(position).get_group() != null) {
                    if (list.get(position).get_group().size() > 0) {
                        for (int i = 0; i < list.get(position).get_group().size(); i++) {
                            if (list.get(position).get_group().get(i).getG_id().equals("6204")) {
                                intent.putExtra("tehui", "1");
                            }
                        }
                    } else {
                        intent.putExtra("tehui", "2");
                    }
                }else{
                    intent.putExtra("tehui", "2");
                }
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onInit() {

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.close_icon:
                finish();
                break;
            case R.id.re_paixu:
                sortPopwindow(v, 1);
                break;
            case R.id.re_shaixuan:
                sortPopwindow(v, 2);
                break;
            case R.id.img_other:
                LoginController.onGouWuChe(this, null);
                break;
        }
    }

    /**
     * 排序弹出框
     *
     * @param v
     * @param type 1、排序    2、筛选金额
     */
    private void sortPopwindow(View v, final int type) {
        if (popupWindow != null && popupWindow.isShowing()) {
            popupWindow.dismiss();
        }
        List<PaiXuBean> list = new ArrayList<>();
        if (type == 1) {
            switch (temp_paixu) {
                case 0:
                    list.add(new PaiXuBean("默认排序", true));
                    list.add(new PaiXuBean("价格从高到底", false));
                    list.add(new PaiXuBean("价格从低到高", false));
                    break;

                case 1:
                    list.add(new PaiXuBean("默认排序", false));
                    list.add(new PaiXuBean("价格从高到底", true));
                    list.add(new PaiXuBean("价格从低到高", false));
                    break;
                case 2:
                    list.add(new PaiXuBean("默认排序", false));
                    list.add(new PaiXuBean("价格从高到底", false));
                    list.add(new PaiXuBean("价格从低到高", true));
                    break;
            }

        } else {
            switch (temp_shaixuan) {
                case 0:
                    list.add(new PaiXuBean("默认", true));
                    list.add(new PaiXuBean("0-1000", false));
                    list.add(new PaiXuBean("1000-3000", false));
                    list.add(new PaiXuBean("3000以上", false));
                    break;

                case 1:
                    list.add(new PaiXuBean("默认", false));
                    list.add(new PaiXuBean("0-1000", true));
                    list.add(new PaiXuBean("1000-3000", false));
                    list.add(new PaiXuBean("3000以上", false));
                    break;
                case 2:
                    list.add(new PaiXuBean("默认", false));
                    list.add(new PaiXuBean("0-1000", false));
                    list.add(new PaiXuBean("1000-3000", true));
                    list.add(new PaiXuBean("3000以上", false));
                    break;
                case 3:
                    list.add(new PaiXuBean("默认", false));
                    list.add(new PaiXuBean("0-1000", false));
                    list.add(new PaiXuBean("1000-3000", false));
                    list.add(new PaiXuBean("3000以上", true));
                    break;
            }

        }

        View view = LayoutInflater.from(this).inflate(R.layout.pop_sort, null);
        ListView listView = (ListView) view.findViewById(R.id.listView);
        listView.setAdapter(new ZuJinAdapter(this, list,
                R.layout.item_simple_list));
        popupWindow = new PopupWindow(view, LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.showAsDropDown(v);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // TODO Auto-generated method stub
                if (type == 1) {
                    temp_paixu = position;
                    if (position == 0) {
                        order = "d";
                    }
                    if (position == 1) {
                        order = "pd";
                    }
                    if (position == 2) {
                        order = "pa";
                    }
                } else if (type == 2) {
                    temp_shaixuan = position;

                    if (position == 0) {
                        l_price = "";
                        r_price = "";
                    }
                    if (position == 1) {
                        l_price = "1";
                        r_price = "100000";
                    }
                    if (position == 2) {
                        l_price = "100000";
                        r_price = "300000";
                    }
                    if (position == 3) {
                        l_price = "300000";
                        r_price = "9999999999999999";
                    }

                }
                presenter.shangPinList(pageIndex + "", size + "", cate_id, "", order, "", l_price, r_price);
                popupWindow.dismiss();
            }
        });
    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            IsUtilUid.tianjia(entity);
            if (entity.getEventType() == ShopMallSearchPresenter.Serach_success) {
                if (pageIndex == 1) {
                    list.clear();
                }
                commend_anchor_ptr.refreshComplete();//设置刷新完成
                shopMallSearchBean = (GoodsModel) entity.getData();
                list.addAll(shopMallSearchBean.getList());
                if (list.size() <= 0) {
                    goods_gridView.setVisibility(View.GONE);
                    null_search.setVisibility(View.VISIBLE);
                } else {
                    adapter.notifyDataSetChanged();
                    goods_gridView.setVisibility(View.VISIBLE);
                    null_search.setVisibility(View.GONE);

                }
            } else if (entity.getEventType() == ShopMallSearchPresenter.Serach_fail) {
                ByAlert.alert(entity.getData() + " ");
            }
        }
    }
}
